//
//  LJHomeViewController.swift
//  LJWeibo
//
//  Created by 连俊杨 on 2017/10/6.
//  Copyright © 2017年 yang_ljun. All rights reserved.
//

import UIKit

fileprivate let homeCellID = "homeCellID"

class LJHomeViewController: LJBaseViewController {
    /// 微博数据视图模型
    fileprivate lazy var listViewModel = LJWBStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func loadData() {

        print("最后一条\(listViewModel.statusList.last)")
        
        listViewModel.loadStatus(pullUp: isPullup) { (isSuccess, shouldRefresh) in
            
            self.isPullup = false
            if shouldRefresh {
                
                self.tableView?.reloadData()
            }
            self.refreshControl?.endRefreshing()
        }
    }
    
    /// MARK: 事件
    @objc fileprivate func showFriends() {
        
        let vc = LJTestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension LJHomeViewController {
    override func setupTableView() {
        super.setupTableView()
        
        /// 所有导航栏的东西都设置在自己的定制导航栏上面
        lj_navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: homeCellID)
        
        setupNavTitleButton()
    }
    
    /// 设置导航栏标题按钮
    fileprivate func setupNavTitleButton() {
        
        /// 设置
        let title = LJNetworkManager.sharedManager.userAccount.screen_name
        let button = LJTitleButton(title: title)
        lj_navigationItem.titleView = button
        
        /// 添加事件
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc fileprivate func clickTitleButton(btn: UIButton) {
        btn.isSelected = !btn.isSelected
    }
}

extension LJHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellID, for: indexPath)
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        return cell
    }
}

